package indi.henry.demo.service.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import indi.henry.demo.model.Counter;
import indi.henry.demo.service.RedisService;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class RedisServiceImpl implements RedisService {

    private final StringRedisTemplate stringRedisTemplate;

    private final RedisTemplate<String, Counter> redisTemplate;

    @Override
    public String getUrlById(String id) {
        return stringRedisTemplate.opsForValue().get(id);
    }

    @Override
    public void setUrlWithId(String id, String url) {
        stringRedisTemplate.opsForValue().set(id, url, 5, TimeUnit.MINUTES);
    }

    @Override
    public Counter getCounter(String counterId) {
        return redisTemplate.opsForValue().get(counterId);
    }

    @Override
    public void setCounter(String counterId, Counter counter) {
        redisTemplate.opsForValue().set(counterId, counter);
    }    
}
